import axios from "axios"
import { message } from "ant-design-vue"
const reg = new RegExp(/\/update$/)

export default {
    get: async (url: string, param?: any) => {
        let result = await axios.get(url, { params: param }).then(res => res.data)

        if (result.code !== 0) {
            console.error(result.msg)
            message.error(result.msg)
            return Promise.reject(new Error(result.msg))
        }

        return result;
    },
    post: async (url: string, param?: any) => {
        // 全局封装
        if (reg.test(url)) {
            if (!param._id) {
                param._id = ""
            }
        }

        let result = await axios.post(url, param).then(res => res.data)

        if (result.code !== 0) {
            console.error(result.msg)
            message.error(result.msg)
            return Promise.reject(new Error(result.msg))
        }

        return result;
    }
}